#include "addplacewid.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "PlaceManger.h"
#include <QMessageBox>
#include <QDebug>
#include <mytool.h>

AddPlaceWid::AddPlaceWid(QWidget *parent) : QWidget(parent)
{
    this->setWindowTitle("新增景点");
    resize(500,300);
    QHBoxLayout *nameLayout = new QHBoxLayout();
    QHBoxLayout *descLayout = new QHBoxLayout();
    QHBoxLayout *xlayout = new QHBoxLayout();
    QHBoxLayout *ylayout = new QHBoxLayout();
    QHBoxLayout *btnlayout = new QHBoxLayout();
    QVBoxLayout *vlayout = new QVBoxLayout(this);

    this->nameLab = new QLabel("地名：",this);
    this->descLab = new QLabel("介绍：",this);
    this->xLab = new QLabel("x坐标：",this);
    this->yLab = new QLabel("y坐标：",this);
    this->editX = new QLineEdit(this);
    editX->setPlaceholderText("0-17以内的数字");
    this->editY = new QLineEdit(this);
    editY->setPlaceholderText("0-12以内的数字");
    this->nameEdit = new QLineEdit(this);
    nameEdit->setPlaceholderText("10个字以内");
    this->descEdit = new QTextEdit(this);
    descEdit->setPlaceholderText("500个字符以内");
    this->confirmBtn = new QPushButton("保存",this);
    this->confirmBtn->setMaximumWidth(200);


    nameLayout->addWidget(nameLab);
    nameLayout->addWidget(nameEdit);

    descLayout->addWidget(descLab);
    descLayout->addWidget(descEdit);

    xlayout->addWidget(xLab);
    xlayout->addWidget(editX);

    ylayout->addWidget(yLab);
    ylayout->addWidget(editY);

    btnlayout->addWidget(confirmBtn);

    vlayout->addLayout(nameLayout);
    vlayout->addLayout(xlayout);
    vlayout->addLayout(ylayout);
    vlayout->addLayout(descLayout);
    vlayout->addLayout(btnlayout);
    vlayout->setAlignment(Qt::AlignCenter);
    vlayout->setMargin(40);
    vlayout->setSpacing(20);


    this->setTabOrder(nameEdit,editX);
    this->setTabOrder(editX,editY);
    this->setTabOrder(editY,descEdit);
    connect(confirmBtn,&QPushButton::released,this,saveData);
}

void AddPlaceWid::saveData()
{
    std::string name;
    std::string desc;
    MyTool tool;
    if(!tool.isDigitStr(editX->text())||!tool.isDigitStr(editY->text()))
    {
        QMessageBox::information(this,"error","您输入的x或y不合法！！",QMessageBox::Yes);
        return ;
    }
    if(this->nameEdit->text().length()>10)
    {
        QMessageBox::information(this,"error","您输入的地名过长！",QMessageBox::Yes);
        return ;
    }
    int x = this->editX->text().toInt();
    int y = this->editY->text().toInt();
    name = (char *)this->nameEdit->text().toStdString().c_str();
    desc = (char *)this->descEdit->toPlainText().toStdString().c_str();
    PlaceManger PM;
    if(PM.Search((char *)name.c_str())!=-1)
    {
        QMessageBox::information(this,"error",QString::fromStdString(name) + "已存在!",QMessageBox::Yes);
        return ;
    }
    Place place(PM.count()+1,(char *)name.c_str(),(char *)desc.c_str());
    place.setX(x);
    place.setY(y);
    int res = QMessageBox::information(this,"ok","您确定要添加吗？",QMessageBox::Yes,QMessageBox::No);
    if(res == QMessageBox::Yes)
    {
        int len;
        int flag = 0;
        Place *places = PM.List(len);
        for(int i = 0;i<len;i++)
        {
            if(places[i].getX()==x&&places[i].getY()==y)
            {
                flag = 1;
                if(strcmp(places[i].getName(),"")!=0)
                {
                    QMessageBox::information(this,"error","此区域已存在景点！",QMessageBox::Yes);
                    return ;
                }
                if(places[i].getUp()==0&&places[i].getDown()==0&&places[i].getRight()==0&&places[i].getRight()==0)
                {
                    QMessageBox::information(this,"error","此区域为绿化带不可添加地点！",QMessageBox::Yes);
                    return ;
                }
            }
        }
        if(!flag)
        {
            QMessageBox::information(this,"error","此区域不可添加地点！",QMessageBox::Yes);
            return ;
        }
        PM.AddNew(place);
        emit dataSaved();
    }
    else
    {

    }
}
